import uuid
from typing import Any

import pytest
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from recruit.adapter.database.gateway import ResumeGateway
from recruit.adapter.database.models import User, Resume
from tests.adapter.gateway.utils import delete_user
from .fixtures.resume import ResumeTest
from .fixtures.user import UserTest


async def _get_resume(
        session: AsyncSession,
        id: uuid.uuid4()
) -> Resume | None:
    stmt = select(Resume).where(Resume.id == id)
    return (await session.execute(stmt)).scalar_one_or_none()


async def _check_resume(session: AsyncSession, data: dict[str, Any]) -> None:
    resume = await _get_resume(session, data['id'])
    assert isinstance(resume, Resume)
    assert resume.id == data['id']
    assert resume.user_id == data['user_id']


@pytest.mark.parametrize(
    'data', [
        {'id': uuid.uuid4()}
    ]
)
async def test_create(
        data: dict[str, uuid.UUID],
        db_session: AsyncSession,
        create_user: UserTest
) -> None:
    resume_gateway = ResumeGateway(db_session)
    user = create_user
    data['user_id'] = user.id
    resume = await resume_gateway.create(**data)
    assert resume.id == data['id']
    assert resume.user_id == data['user_id']
    assert isinstance(resume.user, User)
    await db_session.commit()
    await _check_resume(db_session, data)
    await delete_user(db_session, data['user_id'])


async def test_get_one(
        db_session: AsyncSession,
        create_resume: ResumeTest
) -> None:
    resume_gateway = ResumeGateway(db_session)
    data = {
        'id': create_resume.id,
        'user_id': create_resume.user_id
    }
    for key, value in data.items():
        resume = await resume_gateway.get_one(**{key: value})
        assert resume is not None
        assert resume.id == create_resume.id
        assert resume.user_id == create_resume.user_id
    await delete_user(db_session, data['user_id'])


async def test_check_resume(
        db_session: AsyncSession,
        create_resume: ResumeTest
) -> None:
    resume_gateway = ResumeGateway(db_session)
    val = await resume_gateway.check_resume(create_resume.id)
    assert val
    await delete_user(db_session, create_resume.user_id)
    val = await resume_gateway.check_resume(create_resume.id)
    assert not val
